import Vue from 'vue'
import iView from 'iview'
import VueRouter from 'vue-router'
import { routers, otherRouter, appRouter } from './router'
import Vuex from 'vuex'
import Util from './libs/util'
import App from './app.vue'
import Cookies from 'js-cookie'
import 'iview/dist/styles/iview.css'

import VueI18n from 'vue-i18n'
import Locales from './locale'
import zhLocale from 'iview/src/locale/lang/zh-CN'
import enLocale from 'iview/src/locale/lang/en-US'
import url from './config/url'
import axios from 'axios'

Vue.use(VueRouter)
Vue.use(Vuex)
Vue.use(VueI18n)
Vue.use(iView)

// 自动设置语言
const navLang = navigator.language
const localLang = (navLang === 'zh-CN' || navLang === 'en-US') ? navLang : false
const lang = window.localStorage.getItem('language') || localLang || 'zh-CN'

Vue.config.lang = lang

// 多语言配置
const locales = Locales
const mergeZH = Object.assign(zhLocale, locales['zh-CN'])
const mergeEN = Object.assign(enLocale, locales['en-US'])
Vue.locale('zh-CN', mergeZH)
Vue.locale('en-US', mergeEN)

// 路由配置
const RouterConfig = {
  // mode: 'history',
  routes: routers
}

const router = new VueRouter(RouterConfig)

router.beforeEach((to, from, next) => {
  iView.LoadingBar.start()
  Util.title(to.meta.title)
  if (Cookies.get('locking') === '1' && to.name !== 'locking') {  // 判断当前是否是锁定状态
    next(false)
    router.replace({
      name: 'locking'
    })
  } else if (Cookies.get('locking') === '0' && to.name === 'locking') {
    next(false)
  } else {
    if (!Cookies.get('user') && to.name !== 'login') {  // 判断是否已经登录且前往的页面不是登录页
      next({
        name: 'login'
      })
    } else if (Cookies.get('user') && to.name === 'login') {  // 判断是否已经登录且前往的是登录页
      Util.title()
      next({
        name: 'home_index'
      })
    } else {
      if (Util.getRouterObjByName([otherRouter, ...appRouter], to.name).access !== undefined) {  // 判断用户是否有权限访问当前页
        if (Util.getRouterObjByName([otherRouter, ...appRouter], to.name).access === parseInt(Cookies.get('access'))) {
          // next();
          Util.toDefaultPage([otherRouter, ...appRouter], to.name, router, next)  // 如果在地址栏输入的是一级菜单则默认打开其第一个二级菜单的页面
        } else {
          router.replace({
            name: 'error_401'
          })
          next()
        }
      } else {
        // next();
        Util.toDefaultPage([otherRouter, ...appRouter], to.name, router, next)
      }
    }
  }
  iView.LoadingBar.finish()
})

router.afterEach(() => {
  iView.LoadingBar.finish()
  window.scrollTo(0, 0)
})
// 状态管理
const store = new Vuex.Store({
  state: {
    routers: [
      otherRouter,
      ...appRouter
    ],
    menuList: [],
    tagsList: [...otherRouter.children],
    pageOpenedList: [],
    currentPageName: '',
    currentPath: [
      {
        title: '首页',
        path: '',
        name: 'home_index'
      }
    ],  // 面包屑数组
    openedSubmenuArr: [],  // 要展开的菜单数组
    menuTheme: '', // 主题
    theme: '',
    cachePage: [],
    access_token: Cookies.get('access_token'),
    user: null
  },
  getters: {},
  mutations: {
    setLoading (state, payload) {
      state.loading = payload.loading
    },
    profile (state, payload) {
      state.user = payload.profile
    },
    token (state, payload) {
      // 变更状态
      state.access_token = payload.access_token
      Cookies.set('access_token', state.access_token)
    },
    setTagsList (state, list) {
      state.tagsList.push(...list)
    },
    closePage (state, name) {
      state.cachePage.forEach((item, index) => {
        if (item === name) {
          state.cachePage.splice(index, 1)
        }
      })
    },
    increateTag (state, tagObj) {
      state.cachePage.push(tagObj.name)
      state.pageOpenedList.push(tagObj)
    },
    initCachepage (state) {
      if (localStorage.pageOpenedList) {
        state.cachePage = JSON.parse(localStorage.pageOpenedList).map(item => {
          if (item.name !== 'home_index') {
            return item.name
          }
        })
      }
    },
    removeTag (state, name) {
      state.pageOpenedList.map((item, index) => {
        if (item.name === name) {
          state.pageOpenedList.splice(index, 1)
        }
      })
    },
    pageOpenedList (state, get) {
      let openedPage = state.pageOpenedList[get.index]
      if (get.argu) {
        openedPage.argu = get.argu
      }
      if (get.query) {
        openedPage.query = get.query
      }
      state.pageOpenedList.splice(get.index, 1, openedPage)
      localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList)
    },
    clearAllTags (state) {
      state.pageOpenedList.splice(1)
      router.push({
        name: 'home_index'
      })
      state.cachePage = []
      localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList)
    },
    clearOtherTags (state, vm) {
      let currentName = vm.$route.name
      let currentIndex = 0
      state.pageOpenedList.forEach((item, index) => {
        if (item.name === currentName) {
          currentIndex = index
        }
      })
      if (currentIndex === 0) {
        state.pageOpenedList.splice(1)
      } else {
        state.pageOpenedList.splice(currentIndex + 1)
        state.pageOpenedList.splice(1, currentIndex - 1)
      }
      let newCachepage = state.cachePage.filter(item => {
        return item === currentName
      })
      state.cachePage = newCachepage
      localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList)
    },
    setOpenedList (state) {
      state.pageOpenedList = localStorage.pageOpenedList ? JSON.parse(localStorage.pageOpenedList) : [otherRouter.children[0]]
    },
    setCurrentPath (state, pathArr) {
      state.currentPath = pathArr
    },
    setCurrentPageName (state, name) {
      state.currentPageName = name
    },
    addOpenSubmenu (state, name) {
      let hasThisName = false
      let isEmpty = false
      if (name.length === 0) {
        isEmpty = true
      }
      if (state.openedSubmenuArr.indexOf(name) > -1) {
        hasThisName = true
      }
      if (!hasThisName && !isEmpty) {
        state.openedSubmenuArr.push(name)
      }
    },
    clearOpenedSubmenu (state) {
      state.openedSubmenuArr.length = 0
    },
    changeMenuTheme (state, theme) {
      state.menuTheme = theme
    },
    changeMainTheme (state, mainTheme) {
      state.theme = mainTheme
    },
    lock (state) {
      Cookies.set('locking', '1')
    },
    unlock (state) {
      Cookies.set('locking', '0')
    },
    setMenuList (state, menulist) {
      state.menuList = menulist
    },
    updateMenulist (state) {
      let accessCode = parseInt(Cookies.get('access'))
      let menuList = []
      appRouter.forEach((item, index) => {
        if (item.access !== undefined) {
          if (Util.showThisRoute(item.access, accessCode)) {
            if (item.children.length === 1) {
              menuList.push(item)
            } else {
              let len = menuList.push(item)
              let childrenArr = []
              childrenArr = item.children.filter(child => {
                if (child.access !== undefined) {
                  if (child.access === accessCode) {
                    return child
                  }
                } else {
                  return child
                }
              })
              menuList[len - 1].children = childrenArr
            }
          }
        } else {
          if (item.children.length === 1) {
            menuList.push(item)
          } else {
            let len = menuList.push(item)
            let childrenArr = []
            childrenArr = item.children.filter(child => {
              if (child.access !== undefined) {
                if (Util.showThisRoute(child.access, accessCode)) {
                  return child
                }
              } else {
                return child
              }
            })
            let handledItem = JSON.parse(JSON.stringify(menuList[len - 1]))
            handledItem.children = childrenArr
            menuList.splice(len - 1, 1, handledItem)
          }
        }
      })
      state.menuList = menuList
    },
    setAvator (state, path) {
      localStorage.avatorImgPath = path
    }
  },
  actions: {
    login ({dispatch, commit}, data) {
      commit('setLoading', {loading: true})
      axios.post(url.login, data).then((res) => {
        console.log(res)
        commit('setLoading', {loading: false})
        if (res.status) {
          let access_token = res.data.access_token
          commit('token', {access_token: access_token})
          Cookies.set('user', data.email)
          Cookies.set('password', data.password)
          router.push('/home')
          dispatch('profile')
        }
      }).catch((_) => {
        commit('setLoading', {loading: false})
      })
    },
    profile ({commit, state}) {
      axios.get(url.profile).then((res) => {
        if (res.status) {
          commit('profile', {profile: res.data})
        }
      })
    },
    clear ({commit, state}) {
      commit('token', {access_token: ''})
      Cookies.set('user', '')
      Cookies.set('password', '')
      Cookies.set('password', '')
      router.push('/login')
    }
  }
})
// axios.defaults.baseURL = url.host

axios.interceptors.request.use((config) => {
  config.headers.Authorization = `Bearer ${store.state.access_token}`
  return config
})
axios.interceptors.response.use((response) => {
  if (response.data) {
    if (response.headers.authorization) {
      let access_token = response.headers.authorization
      console.log(access_token)
      if (access_token) {
        access_token = access_token.replace('Bearer ', '')
        store.commit('token', {access_token: access_token})
      }
    }
    return response.data
  } else {
    return response
  }
}, (error) => {
  store.commit('setLoading', {loading: false})
  if (error.response.headers.authorization) {
    let access_token = error.response.headers.authorization
    if (access_token) {
      access_token = access_token.replace('Bearer ', '')
      store.commit('token', {access_token: access_token})
    }
  }
  if (error.response.data) {
    console.log(error.response.data)
    if (error.response.status === 422) {
      iView.Notice.error({
        title: '错误',
        desc: error.response.data.message
      })
    } else {
      iView.Notice.error({
        title: '错误',
        desc: error.response.data.message
      })
    }

    if (error.response.status == 401) {
      store.dispatch('clear')
    }
    return error.response.data
  } else {
    return error
  }
})
window.$api = axios
new Vue({
  el: '#app',
  router: router,
  store: store,
  render: h => h(App),
  data: {
    currentPageName: ''
  },
  mounted () {
    this.currentPageName = this.$route.name
    this.$store.commit('initCachepage')
    // 权限菜单过滤相关
    this.$store.commit('updateMenulist')
  },
  created () {
    let tagsList = []
    appRouter.map((item) => {
      if (item.children.length <= 1) {
        tagsList.push(item.children[0])
      } else {
        tagsList.push(...item.children)
      }
    })
    this.$store.commit('setTagsList', tagsList)
  },
  watch: {
    '$route' (to) {
      // if (Util.getRouterObjByName(this, to.name).access) {
      //     if (Util.getRouterObjByName(this, to.name).access === parseInt(Cookies.get('access'))) {

      //     }
      // }
    }
  }
})
